import {
    defineStore
} from 'pinia'


export const userGoodsStore = defineStore({
    id: 'goods',
    state: () => ({
        //相关变量
        cartData: [],
        data:[]
    }),
    actions: {
        //创建相关方法
        cartAdd(val) {
            let temp = JSON.parse(sessionStorage.getItem('cart'))
            if (temp == null) {
                this.cartData.push(val)
            } else {
                this.cartData = temp
                this.data = this.cartData.find(v => v.id == val.id) //查找新插入的商品编号是否在原来的购物车
                //存在则，则进行数量累加,重新计算商品总金额
                if (this.data) {
                    this.data.num += val.num
                    this.data.total = this.data.num * this.data.onlineprice
                } else {
                    this.cartData.push(val)
                }

            }
            sessionStorage.setItem('cart', JSON.stringify(this.cartData))
        },
        //创建生成订单的方法
        orderAdd(val){
                this.orderAdd.push(val)
        }
    }
})